import datetime
from xml.etree import ElementTree
from models.invoice import Invoice
from models.company import Company



class Backup():
	@classmethod
	def export(cls):
			result = ('<data>\n')
			result += ('\t<companies>\n')
			for n in Company.all():
				result += ('\t\t<company>\n')
				result += ('\t\t\t<name>'        + str(n.name) + '</name>\n')
				result += ('\t\t\t<slug>'        + str(n.slug) + '</slug>\n')
				result += ('\t\t\t<email>'       + str(n.email) + '</email>\n')
				result += ('\t\t\t<phone>'       + str(n.phone) + '</phone>\n')
				result += ('\t\t\t<address>'     + str(n.address) + '</address>\n')
				result += ('\t\t\t<website>'     + str(n.website) + '</website>\n')
				result += ('\t\t\t<description>' + str(n.description) + '</description>\n')
				result += ('\t\t</company>\n')
			result += ('\t</companies>\n')
			result += ('\t<invoices>\n')
			for n in Invoice.all().order('created'):
				result += ('\t\t<invoice>\n')
				result += ('\t\t\t<number>'    + str(n.number)   + '</number>\n')
				result += ('\t\t\t<company>'   + str(n.company)  + '</company>\n')
				result += ('\t\t\t<comments>'  + str(n.comments) + '</comments>\n')
				result += ('\t\t\t<created>'   + n.created.strftime('%m/%d/%Y %I:%M:%S %p') + '</created>\n')
				result += ('\t\t\t<startDate>' + n.startDate.strftime('%m/%d/%Y %I:%M:%S %p') + '</startDate>\n')
				result += ('\t\t\t<endDate>'   + n.endDate.strftime('%m/%d/%Y %I:%M:%S %p') + '</endDate>\n')
				result += ('\t\t\t<hours>'     + str(n.hours)   + '</hours>\n')
				result += ('\t\t\t<rate>'      + str(n.rate)    + '</rate>\n')
				result += ('\t\t\t<taxRate>'   + str(n.taxRate) + '</taxRate>\n')
				result += ('\t\t\t<paid>'      + str(n.paid)    + '</paid>\n')
				result += ('\t\t\t<sent>'      + str(n.sent)    + '</sent>\n')
				result += ('\t\t</invoice>\n')
			result += ('\t</invoices>\n')
			result += ('</data>')
			return result
	
	@classmethod
	def load(cls, data):
		invoice_create = 0
		invoice_update = 0
		company_create = 0
		company_update = 0
		document = ElementTree.fromstring(data)
		
		#import company
		for e in document.getiterator('company'):
			for eslug in e.getiterator("slug"):
				company = Company.get_by_slug( str(eslug.text) )
				if not company:
					company = Company()
					company.slug = str(eslug.text)
					company_create+=1
				else:
					company_update+=1
				for i in e.getiterator("name"):
					company.name = str(i.text)
				for i in e.getiterator("email"):
					company.email =  str(i.text)
				for i in e.getiterator("phone"):
					company.phone =  str(i.text)
				for i in e.getiterator("address"):
					company.address =  str(i.text)
				for i in e.getiterator("website"):
					company.website =  str(i.text)					
				for i in e.getiterator("description"):
					company.description =  str(i.text)	
				company.put()
				
		#import invoice
		for e in document.getiterator('invoice'):
			for enumber in e.getiterator("number"):
				invoice = Invoice.get_by_number( str(enumber.text) )
				if not invoice:
					invoice = Invoice()
					invoice.number = str(enumber.text)
					invoice_create+=1
				else:
					invoice_update+=1
				for i in e.getiterator("company"):
					invoice.company = str(i.text)
				for i in e.getiterator("comments"):
					invoice.comments = str(i.text)
				for i in e.getiterator("hours"):
					invoice.hours =  float(i.text)
				for i in e.getiterator("rate"):
					invoice.rate =  float(i.text)
				for i in e.getiterator("taxRate"):
					invoice.taxRate = float(i.text)
				for i in e.getiterator("paid"):
					invoice.paid =  str(i.text) == 'True'
				for i in e.getiterator("sent"):
					invoice.sent =  str(i.text) == 'True'		
				for i in e.getiterator("created"):
					invoice.created = datetime.datetime.strptime(str(i.text), "%m/%d/%Y %I:%M:%S %p")
				for i in e.getiterator("startDate"):
					invoice.startDate =  datetime.datetime.strptime(str(i.text), "%m/%d/%Y %I:%M:%S %p")
				for i in e.getiterator("endDate"):
					invoice.endDate =  datetime.datetime.strptime(str(i.text), "%m/%d/%Y %I:%M:%S %p")				
				invoice.range = [invoice.startDate, invoice.endDate]
				invoice.put()
		return {'invoice_create' : invoice_create,
				'invoice_update' : invoice_update,
				'company_create' : company_create,
				'company_update' : company_update}

"""
	old import code - from .net application

		path    = os.path.join(os.path.dirname(__file__), 'data/invoice-data.xml')
		file      = open(path)
		document = ElementTree.fromstring(file.read())
		for e in document.getiterator('Invoice'):
			invoice = Invoice()
			invoice.company = 'spitfire-creative'
			for i in e.getiterator("InvoiceID"):
				invoice.number = str(i.text)
			for i in e.getiterator("Comments"):
				invoice.comments = str(i.text)
			for i in e.getiterator("Hours"):
				invoice.hours =  float(i.text)
			for i in e.getiterator("Rate"):
				invoice.rate =  float(i.text)
			for i in e.getiterator("TaxRate"):
				invoice.taxRate = float(i.text) * 0.01
			for i in e.getiterator("Paid"):
				invoice.paid =  str(i.text) == 'True'
			for i in e.getiterator("Sent"):
				invoice.sent =  str(i.text) == 'True'		
			for i in e.getiterator("Created"):
				invoice.created = datetime.datetime.strptime(str(i.text), "%m/%d/%Y %I:%M:%S %p")
			for i in e.getiterator("StartDate"):
				invoice.startDate =  datetime.datetime.strptime(str(i.text), "%m/%d/%Y %I:%M:%S %p")
			for i in e.getiterator("EndDate"):
				invoice.endDate =  datetime.datetime.strptime(str(i.text), "%m/%d/%Y %I:%M:%S %p")				
			invoice.range = [invoice.startDate, invoice.endDate]
			#invoice.put()
		file.close()

"""		
		